package com.hwl.eduservice.controller;

import com.hwl.commonutils.result.RespBean;
import com.hwl.eduservice.pojo.Chapter;
import com.hwl.eduservice.pojo.frontVo.WebCourseQueryVo;
import com.hwl.eduservice.pojo.frontVo.WebCourseVo;
import com.hwl.eduservice.service.ChapterService;
import com.hwl.eduservice.service.CourseService;
import com.hwl.servicebase.dto.CourseDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "课程")
@RestController
@RequestMapping("/api/eduCourse")
public class ApiCourseController {

    @Resource
    private CourseService courseService;
    @Resource
    private ChapterService chapterService;

    @ApiOperation("课程列表")
    @GetMapping("list")
    public RespBean pageList(WebCourseQueryVo webCourseQueryVo) {
        return RespBean.ok().data(courseService.webSelectList(webCourseQueryVo));
    }

    @ApiOperation("根据id查询课程")
    @GetMapping("get/{id}")
    public RespBean getById(@PathVariable String id) {
        WebCourseVo webCourseVo = courseService.selectCourseById(id);
        List<Chapter> chapters = chapterService.getAllChapter(id);

        Map<String, Object> map = new HashMap<>();
        map.put("course", webCourseVo);
        map.put("chapterList", chapters);

        return RespBean.ok().data(map);
    }

    @ApiOperation("根据课程id查询课程信息")
    @GetMapping("inner/getCourseDto/{courseId}")
    public CourseDto getCourseDtoById(@PathVariable String courseId){
        return courseService.getCourseDtoById(courseId);
    }

    @ApiOperation("根据课程id更新销量")
    @GetMapping("inner/updateBuyCount/{id}")
    public RespBean updateCourseBuyCount(@PathVariable String id){
        courseService.updateBuyCountById(id);
        return RespBean.ok().message("更新课程销量成功！");
    }
}
